package com.jbm.jbmsupplier.zp.service;

import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.Log;

import com.jbm.core.client.MainCallback;
import com.jbm.core.common.ConnectAgent;
import com.jbm.core.iface.IFileObject;
import com.jbm.jbmsupplier.shared.SharedPreferActivity;
import com.jbm.jbmsupplier.zp.entity.ChatMsgEntity;
import com.jbm.jbmsupplier.zp.model.MyServiceModel;
import com.jbm.jbmsupplier.zp.presenters.MyServices;
import com.jbm.jbmsupplier.zp.utils.ClassUser;
import com.jbm.jbmsupplier.zp.views.MyServiceView;
import com.jbm.onehome.iface.CallGroupEntity;
import com.jbm.onehome.iface.ContactEntity;
import com.jbm.onehome.iface.GoodsEntity;
import com.jbm.onehome.iface.ICertAddress;
import com.jbm.onehome.iface.IorderEntity;
import com.jbm.onehome.iface.JobOrderEntity;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class MyService extends Service implements MyServiceView {
    private  MainCallback callback;
    private final ArrayList<ChatMsgEntity> arrayList;
    private ArrayList<HashMap<String, String>> list1;
    public  static boolean isOn = true;
    private MyServiceModel model;
    private MyServices myServices=null;
    private ClassUser classuser;

    @Override
    public void onCreate() {
        super.onCreate();
        if(myServices==null){
            myServices=new MyServices(this,this);
        }
          myServices.InitData(1);
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if(myServices==null){
            myServices=new MyServices(this,this);}
            classuser=new ClassUser(getBaseContext());
            myServices.InitData(2);
//        startForeground(12346, model.getNotification("", "", 0, getBaseContext(), 0));
        //当前好友id
        return Service.START_STICKY;
    }
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public void setUpdata(String name) {//连接服务器
        if(name.equals("关机")){
            model = new MyServiceModel(this, callback,"关机");
        }else if (name.equals("开机")){
            model = new MyServiceModel(this, callback,"开关");
        }
    }
    private void setSaveData(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                        synchronized (arrayList) {
                            if (arrayList!= null) {
                            if(arrayList.size()<=0){
                               }else {
                            if (arrayList.size() >= 1) {
                                try {
                                    switch (arrayList.get(0).getFlag()) {
                                        case 8:
                                            myServices.SaveData(arrayList,8);//文本消息
                                            break;
                                        case 10:
                                            myServices.SaveData(arrayList,10);//
                                            break;
                                        case 12:
                                            myServices.SaveData(arrayList,12);
                                            break;
                                        case 14:
                                            myServices.SaveData(arrayList,14);
                                            break;
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                                }
                            }
                            }
                        }
                }
            }
        }).start();
    }
    @Override
    public void getAllData(ChatMsgEntity chatmsg) {//获取数据保存数据库
        arrayList.add(chatmsg);

    }
    public MyService() {
        arrayList = new ArrayList<>();
//           setSaveData();
         callback = new MainCallback() {
            @Override
            public void NewAccount(ConnectAgent ca) {//添加新好友

            }
            @Override
            public void RemoveAccount(Integer account_id) {//删除好友

            }
            @Override
            public void UpdateAccount(boolean pullall, ConnectAgent ca) {//更新好友状态

            }
            @Override
            public void OfflineAccount(int status, ConnectAgent ca) {//好友离线状态

            }
            @Override
            public void NewDevice(ConnectAgent ca) {

            }
            @Override
            public void RemoveDevice(Integer account_id) {
            }
            @Override
            public void UpdateDevice(boolean pullall, ConnectAgent ca) {
            }
            @Override
            public void OfflineDevice(ConnectAgent ca) {
            }
             @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
             @Override
             public void NewText(final Integer account_id, String text, long timestamp) {
//
//                 myServices.NewText(account_id,text,timestamp);
//                 Buddy_ListData names = MySqliteUtlis.GetInstan(getApplicationContext()).showFriendsToID(BUDDY, account_id, classuser.getLogin());
//                 if (names == null) {
//                     startForeground(12346, model.getNotification(account_id+"....", text, 1, getBaseContext(),account_id));
//                 }else {
//                     startForeground(12346, model.getNotification(names.getNickname(), text, 1, getBaseContext(),account_id));
//                 }
             }

             @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
             @Override
             public void NewFile(final Integer account_id, IFileObject fo, long timestamp) {
//                 myServices.NewFile(account_id,fo,timestamp);
//                 Buddy_ListData names = MySqliteUtlis.GetInstan(getApplicationContext()).showFriendsToID(BUDDY, account_id, classuser.getLogin());
//                 if (fo.filetype.equals("pic")) {
//                     if (names == null) {
//                         startForeground(12346, model.getNotification(account_id+"....", "图片", 1, getBaseContext(),account_id));
//                     }else {
//                         startForeground(12346, model.getNotification(names.getNickname(), "图片", 1, getBaseContext(),account_id));
//                     }
//                 } else if (fo.filetype.equals("audio")) {
//                     if (names == null) {
//                         startForeground(12346, model.getNotification("....", "语音", 1, getBaseContext(),account_id));
//                     }else {
//                         startForeground(12346, model.getNotification(names.getNickname(), "语音", 1, getBaseContext(),account_id));
//                     }
//                 }
             }
             @Override                                    //消息      //A钱                 //类型
             public void NewPay(Integer account_id, String msg, BigDecimal fee, String paytype, long timestamp) {
                 myServices.NewPay(account_id, msg, fee, paytype, timestamp);
             }
             @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
             @Override
             public void CGNewText(int cg_id, Integer account_id, String text, long timestamp) {//接收群文本的消息
//                 if (account_id!=classuser.getLogin()) {
//                     myServices.CGNewText(cg_id, account_id, text, timestamp);//调用接口传递群文本信息
//                     Recent_ContactsData grname = MySqliteUtlis.GetInstan(getBaseContext()).SelectName(GROUD, cg_id);
//                     if (grname == null) {
//                         MySqliteUtlis.GetInstan(getBaseContext()).InsertGroad(GROUD, "群组名" + cg_id + "创建人" + classuser.getLogin(), "1", getDate() + "", "群消息", 0, cg_id, 1,classuser.getLogin());
//                     } else if (!grname.getName().contains("群组名" + cg_id + "创建人" + classuser.getLogin())) {
//                         MySqliteUtlis.GetInstan(getBaseContext()).InsertGroad(GROUD, "群组名" + cg_id + "创建人" + classuser.getLogin(), "1", getDate() + "", "群消息", 0, cg_id, 1, classuser.getLogin());
//                     }
//                     startForeground(12346, model.getNotification(grname.getName(), text, 1, getBaseContext(), account_id));
//                 }
             }
             @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
             @Override
             public void CGNewFile(int cg_id, Integer account_id, IFileObject fo, long timestamp) {//接收群文件的消息
//                 if (account_id!=classuser.getLogin()) {
//                   myServices.CGNewFile(cg_id,account_id,fo,timestamp);//调用接口传递群文件信息
//                     Recent_ContactsData grname = MySqliteUtlis.GetInstan(getBaseContext()).SelectName(GROUD, cg_id);
//                     if (grname==null){
//                         MySqliteUtlis.GetInstan(getBaseContext()).InsertGroad(GROUD, "群组名" + cg_id + "创建人" + classuser.getLogin(), "1", getDate() + "", "群消息", 0, cg_id, 1,classuser.getLogin());
//                     }else if(!grname.getName().contains("群组名" + cg_id + "创建人" +new ClassUser(getBaseContext()).getLogin())) {
//                         MySqliteUtlis.GetInstan(getBaseContext()).InsertGroad(GROUD, "群组名" + cg_id + "创建人" + classuser.getLogin(), "1", getDate() + "", "群消息", 0, cg_id, 1, classuser.getLogin());
//                     }
//                 if (fo.filetype.equals("pic")) {
//                     startForeground(12346, model.getNotification(grname.getName(), "图片", 1, getBaseContext(),account_id));
//                 } else if (fo.filetype.equals("audio")) {
//                     startForeground(12346, model.getNotification(grname.getName(), "语音", 1, getBaseContext(),account_id));
//
//                 }
//
//                 }
             }

             @Override
             public void orderStatus(IorderEntity iorderEntity) {//订单数据
                 //temp   在用户端报修成功生成订单是
              Log.e("main","_____orderStatus_______"+iorderEntity.getStatus());

             }
             @Override
             public void jobOrderStatus(JobOrderEntity jobOrderEntity) {//工单数据
                 Log.e("main","jobOrderStatus"+jobOrderEntity.getStatus());
             }
             @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
             @Override
             public void addToUnit(CallGroupEntity cg) {//有新的用户创建群
                 myServices.addToUnit(cg);
//                 startForeground(12346,model.getNotification(cg.getName(), "一条新订单", 1, getBaseContext(),cg.getCgId()));
             }

             @Override
            public void AddContact(ContactEntity ce) {
             myServices.AddContact(ce);
            }
            @Override
            public void DropContact(ContactEntity ce) {
                myServices.DropContact(ce);
            }

            @Override
            public void Belogout() {//强制退出

                if(new SharedPreferActivity(getApplication()).getBoolearLogin()==true){
                    Intent intin = new Intent();
                    intin.setAction("EXIT");
                    getBaseContext().sendBroadcast(intin);
                }




            }
            @Override
            public List<String> getAllIFace() {
                return Arrays.asList(("com.jbm.core.iface.IAcross|"+
                        "com.jbm.core.iface.ICallGroup|"+
                        "com.jbm.core.iface.IConnectAgent|"+
                        "com.jbm.core.iface.IFileObject|"+
                        "com.jbm.core.iface.IP2P|"+
                        "com.jbm.core.iface.IRegister|"+
                        "com.jbm.onehome.iface.AccSecretEntity|"+
                        "com.jbm.onehome.iface.AlipaylogEntity|"+
                        "com.jbm.onehome.iface.AppJudgeEntity|"+
                        "com.jbm.onehome.iface.BalanceEntity|"+
                        "com.jbm.onehome.iface.BusAuditLogEntity|"+
                        "com.jbm.onehome.iface.BusinessEntity|"+
                        "com.jbm.onehome.iface.CapitalAccEntity|"+
                        "com.jbm.onehome.iface.CertAddressEntity|"+
                        "com.jbm.onehome.iface.ClickTimesEntity|"+
                        "com.jbm.onehome.iface.ContactEntity|"+
                        "com.jbm.onehome.iface.EguardBtEntity|"+
                        "com.jbm.onehome.iface.EguardKeyEntity|"+
                        "com.jbm.onehome.iface.EguardLogEntity|"+
                        "com.jbm.onehome.iface.EnchashmentEntity|"+
                        "com.jbm.onehome.iface.EntranceGuardEntity|"+
                        "com.jbm.onehome.iface.ExchangeEntity|"+
                        "com.jbm.onehome.iface.ExProductEntity|"+
                        "com.jbm.onehome.iface.FocusEntity|"+
                        "com.jbm.onehome.iface.GeoInfoEntity|"+
                        "com.jbm.onehome.iface.GiGiHstyEntity|"+
                        "com.jbm.onehome.iface.GoodsEntity|"+
                        "com.jbm.onehome.iface.IAccCtrl|"+
                        "com.jbm.onehome.iface.IAcc|"+
                        "com.jbm.onehome.iface.IAplipay|"+
                        "com.jbm.onehome.iface.IAPPJudge|"+
                        "com.jbm.onehome.iface.IAppVersion|"+
                        "com.jbm.onehome.iface.IAttachment|"+
                        "com.jbm.onehome.iface.IBreakJob|"+
                        "com.jbm.onehome.iface.IBusinessCtrl|"+
                        "com.jbm.onehome.iface.ICertAddressCtrl|"+
                        "com.jbm.onehome.iface.ICertAddress|"+
                        "com.jbm.onehome.iface.IConfirmJob|"+
                        "com.jbm.onehome.iface.IDownloadMedia|"+
                        "com.jbm.onehome.iface.IEGLogCtrl|"+
                        "com.jbm.onehome.iface.IEGLog|"+
                        "com.jbm.onehome.iface.IEGuard|"+
                        "com.jbm.onehome.iface.IEGuardKeyCtrl|"+
                        "com.jbm.onehome.iface.IEGuardKey|"+
                        "com.jbm.onehome.iface.IExChangeCtrl|"+
                        "com.jbm.onehome.iface.IGeoEntityCtrl|"+
                        "com.jbm.onehome.iface.IGeoinfoCtrl|"+
                        "com.jbm.onehome.iface.IGeoinfo|"+
                        "com.jbm.onehome.iface.IGetJobList|"+
                        "com.jbm.onehome.iface.IInformationCtrl|"+
                        "com.jbm.onehome.iface.IJobTotal|"+
                        "com.jbm.onehome.iface.IJOrderCtrl|"+
                        "com.jbm.onehome.iface.IJOrder|"+
                        "com.jbm.onehome.iface.ILoadJob|"+
                        "com.jbm.onehome.iface.ILoginCtrl|"+
                        "com.jbm.onehome.iface.ILogin|"+
                        "com.jbm.onehome.iface.InfoAttachmentEntity|"+
                        "com.jbm.onehome.iface.InfoAuditLogEntity|"+
                        "com.jbm.onehome.iface.InfoGeoPriceEntity|"+
                        "com.jbm.onehome.iface.InfomationEntity|"+
                        "com.jbm.onehome.iface.InfoScopeEntity|"+
                        "com.jbm.onehome.iface.IOrderCtrl|"+
                        "com.jbm.onehome.iface.IorderEntity|"+
                        "com.jbm.onehome.iface.IOrder|"+
                        "com.jbm.onehome.iface.IOtherConfigCtrl|"+
                        "com.jbm.onehome.iface.IPayCtrl|"+
                        "com.jbm.onehome.iface.IPay|"+
                        "com.jbm.onehome.iface.IpointsCtrl|"+
                        "com.jbm.onehome.iface.IRegistCustomer|"+
                        "com.jbm.onehome.iface.ISaleActivity|"+
                        "com.jbm.onehome.iface.ISecretCtrl|"+
                        "com.jbm.onehome.iface.ISecret|"+
                        "com.jbm.onehome.iface.ISellerCtrl|"+
                        "com.jbm.onehome.iface.ISeller|"+
                        "com.jbm.onehome.iface.IStatBrokenEGuard|"+
                        "com.jbm.onehome.iface.IStatCtrl|"+
                        "com.jbm.onehome.iface.IStatDayEGuard|"+
                        "com.jbm.onehome.iface.IStatEGuardLog|"+
                        "com.jbm.onehome.iface.IStatVillage|"+
                        "com.jbm.onehome.iface.ISubmitJob|"+
                        "com.jbm.onehome.iface.ITotal|"+
                        "com.jbm.onehome.iface.ITrafficRstr|"+
                        "com.jbm.onehome.iface.IUploadMedia|"+
                        "com.jbm.onehome.iface.IVerifyCode|"+
                        "com.jbm.onehome.iface.IVersion|"+
                        "com.jbm.onehome.iface.IWeatherCtrl|"+
                        "com.jbm.onehome.iface.IWeather|"+
                        "com.jbm.onehome.iface.JobOrderEntity|"+
                        "com.jbm.onehome.iface.JordAttachmentEntity|"+
                        "com.jbm.onehome.iface.LoginEntity|"+
                        "com.jbm.onehome.iface.Login|"+
                        "com.jbm.onehome.iface.LoginLogEntity|"+
                        "com.jbm.onehome.iface.LoginRoleEntity|"+
                        "com.jbm.onehome.iface.Media|"+
                        "com.jbm.onehome.iface.MemberEntity|"+
                        "com.jbm.onehome.iface.MemLoginHstyEntity|"+
                        "com.jbm.onehome.iface.MGiEntity|"+
                        "com.jbm.onehome.iface.OrdAttachmentEntity|"+
                        "com.jbm.onehome.iface.OrderItemsEntity|"+
                        "com.jbm.onehome.iface.OtherconfigEntity|"+
                        "com.jbm.onehome.iface.PaybackEntity|"+
                        "com.jbm.onehome.iface.PicInputStream|"+
                        "com.jbm.onehome.iface.PicOutputStream|"+
                        "com.jbm.onehome.iface.PointsEntity|"+
                        "com.jbm.onehome.iface.PointsRuleEntity|"+
                        "com.jbm.onehome.iface.PraiseEntity|"+
                        "com.jbm.onehome.iface.RoleEntity|"+
                        "com.jbm.onehome.iface.SellerEntity|"+
                        "com.jbm.onehome.iface.SellerLoginHstyEntity|"+
                        "com.jbm.onehome.iface.StatisticalReportEntity|"+
                        "com.jbm.onehome.iface.TrafficrstrEntity|"+
                        "com.jbm.onehome.iface.VEguardLogEntity|"+
                        "com.jbm.onehome.iface.VerifyCodeEntity|"+
                        "com.jbm.onehome.iface.VoucherEntity|"+
                        "com.jbm.onehome.iface.WeatherEntity|"+
                        "com.jbm.onehome.iface.WxpaylogEntity|"+
                        "com.jbm.onehome.iface.CallGroupEntity|"+
                        "com.jbm.onehome.iface.CgUnitEntity|"+
                        "com.jbm.onehome.iface.MFunGroupEntity|"+
                        "com.jbm.onehome.iface.MPrivilegesEntity|"+
                        "com.jbm.onehome.iface.PrivilegeListEntity|"+
                        "com.jbm.onehome.iface.FunGroupEntity|"+
                        "com.jbm.onehome.iface.FunItemsEntity|"+
                        "com.jbm.onehome.iface.RoleFunctionEntity|"+
                        "com.jbm.onehome.iface.GoodsClassEntity").split("\\|"));
            }

             @Override
             public void ReConnect() {//没有链接网络
//                 getBaseContext().startService(new Intent(getBaseContext(), MyService.class));
//                 new SharedPreferActivity(getBaseContext()).setBoolearNetConn(false);//网络断开后从新链接
             }

             @Override
            public void GPConfirmStart(int cg_id){
            }

            @Override
            public void GPSelectCertAddress(int cg_id, ICertAddress[] ca) {//小秘书,客户联系地址

            }

             @Override
             public void GPSelectCatalog(int cg_id, List<GoodsEntity> glist) {

             }
            @Override
            public void GPSelectTeam(int cg_id, String[] team) {//小秘书,施工队

            }
             @Override
             public void DeliverCode(String code, String memo, Integer from_id, Integer to_id, long timestamp) {

             }


         };
    }

}
